﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Atlepage
{
    public class TokenBase
    {
        public int Type;
        public string Value;
        public int Location;
        public int Line;
        public int Column;
    }

    public class Token<T> : TokenBase, IComparable<Token<T>>, IEquatable<Token<T>>, IComparable<String>
        where T : Token<T>, new()
    {
        public Lexical.Lexer<T> Lexer;

        public virtual void Load(Lexical.Lexer<T> Lexer, int Type, string Value, int Location, int Line, int Column)
        {
            this.Lexer = Lexer;
            this.Type = Type;
            this.Value = Value;
            this.Location = Location;
            this.Line = Line;
            this.Column = Column;
        }

        public override string ToString()
        {
            return string.Format("TOKEN(Type:{0}, Value:'{1}', Line:{2}, Column:{3})", Type, Value.Replace(@"\", @"\\").Replace("\'", @"\'").Replace("\n", @"\n"), Line, Column);
        }

        #region IComparable<Token<T>> Members

        public virtual int CompareTo(Token<T> other)
        {
            int i = Type.CompareTo(other.Type);
            if (i == 0)
                return Value.CompareTo(other.Value);
            return i;
        }

        #endregion

        #region IEquatable<Token<T>> Members

        public virtual bool Equals(Token<T> other)
        {
            if (object.ReferenceEquals(this, other))
                return true;
            return this.Type.CompareTo(other.Type) == 0 && this.Value == other.Value;
        }

        #endregion

        #region IComparable<string> Members

        public virtual int CompareTo(string other)
        {
            return Type.CompareTo(other);
        }

        #endregion
    }
}
